import time
import allure
import pytest
import requests
import os
import pymysql
basedir=os.path.dirname(__file__)
filepath=os.path.join(basedir,"testCase/updateRole.json")
from .data import load_updateRoledata,baseurl
def sqltest(sql,par=None):
    connect=pymysql.connect(
        host="localhost",      # 数据库地址
        user="root",           # 用户名
        password="202267",     # 密码
        database="ecg_platform",  # 数据库名
        port=3306,             # 端口号（默认3306）
        charset="utf8mb4"      # 推荐使用 utf8mb4 支持Emoji等字符
    )
    cursor=connect.cursor()
    cursor.execute(sql,par)
    res=cursor.fetchall()
    cursor.close()
    connect.close()
    return res
@allure.epic("修改用户角色")
class TestUpdateRole:
    def setup_class(self):
        self.session = requests.session()
        self.url = f"{baseurl}/users/update_role"
        print("修改用户角色接口测试开始：", time.strftime("%Y-%m-%d-%H:%M:%S"))
    def teardown_class(self):
        self.session.close()
        print("修改用户角色接口测试结束：", time.strftime("%Y-%m-%d-%H:%M:%S"))
    @allure.story("超级管理员分配权限")
    @pytest.mark.parametrize("testCase,username1,password,username2,role,msg,status_code",load_updateRoledata(filepath))
    @allure.title("{testCase}")
    def test_SuperadminUpdateRole(self,testCase,username1,password,username2,role,msg,status_code):
        self.session.post(f"{baseurl}/login",data={"username":username1,"password":password})
        data={
            "username":username2,
            "role":role
        }
        responses=self.session.post(self.url,data=data)
        assert 200==responses.status_code
        assert msg in responses.text

        self.session.cookies.clear()


